<?php

namespace App\Http\Requests;

use App\Common\Status;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class BusinessRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    public function rules()
    {
        return [
            'name'       => 'required|string|max:60',
            'short_name' => 'required|string|max:30',
            'logo'       => 'required|integer',
            'logo2'       => 'required|integer',
            'scale'      => 'required|integer',
            'type'       => 'required|integer',
            'content'    => 'required|string',
            'hot'        => 'nullable|integer',
            'show'       => 'nullable|integer',
            'id'         => 'nullable|integer',
            'address'    => 'required|array',
        ];
    }

    public function messages(): array
    {
        return [

            'name.required'       => '企业名称必须',
            'name.max'            => '企业名称不可大于60个字',
            'short_name.required' => '企业短名称必须',
            'short_name.max'      => '企业短名称不可大于30个字',
            'logo.required'       => 'logo必须',
            'logo.integer'        => 'logo必须整形',
            'logo2.required'       => 'logo2必须',
            'logo2.integer'        => 'logo2必须整形',
            'scale.required'      => '企业规模必须',
            'type.required'       => '企业类型必须',
            'content.required'    => '企业简介必须',
//            'content.max'         => '企业简介不可超过800字',
            'address.required'    => '企业地址必须',
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        $error = $validator->errors()->all();
        throw new HttpResponseException($this->fail(Status::ERROR, $error));
    }

    protected function fail(int $code, array $errors)
    {
        return response()->json(
            [
                'code'   => $code,
                'errors' => $errors,
            ]
        );
    }
}

